package com.gdpu.pbss.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.pbss.common.Result;
import com.gdpu.pbss.pojo.Order;
import com.gdpu.pbss.pojo.Pb;
import com.gdpu.pbss.pojo.Pbss;
import com.gdpu.pbss.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/page")
    public Result selectOrderPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String userName,
                                 @RequestParam(defaultValue = "")  Integer orderId
    )
    {


        IPage<Order> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Order> qw =new QueryWrapper<>();

        qw.eq(orderId!=null,"order_id",orderId);
        qw.like(userName!=null,"user_name",userName);
//        if (!"".equals(pbssName)) {
//            qw.like("pbss_name", pbssName);
//        }
        qw.orderByDesc("create_time");


        return Result.success(orderService.page(page,qw));

    }

    @PostMapping("/save")
    public Result saveOrder(@RequestBody Order order){
        if (order.getOrderId()==null){
            return  Result.success(orderService.save(order))   ;
        }else {
            if (order.getOrderStatus()==true){
                Date date = new Date();
                    Timestamp nuptime = new Timestamp(date.getTime());
                    order.setEndTime(nuptime);
            }



            return Result.success(orderService.updateById(order)) ;
        }
    }

    @DeleteMapping("/{orderId}")
    public Result delOrder(@PathVariable("orderId") Integer orderId){
        orderService.removeById(orderId);
        return Result.success();
    }


    @PostMapping("/del/batch")
    public Result delBatchOrder(@RequestBody List<Integer> ids){
        QueryWrapper<Order> qw=new QueryWrapper<>();
        qw.in("order_id",ids);
        List<Order> list = orderService.list(qw);
        for (Order order : list) {
            orderService.removeById(order.getOrderId());
        }
        return Result.success();
    }

}
